package com.tongfusoft.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

/**
 * 按照设定的日期格式对json中的日期进行转换
 * 
 * @author shadow guo
 * 
 */

public class JsonDateValueProcessor implements JsonValueProcessor{
private String format = "yyyy-MM-dd HH:mm:ss";     
    
    public JsonDateValueProcessor() {     
    
    }     
    
    /**
     * 构造方法
     * @param format  日期格式
     */
    public JsonDateValueProcessor(String format) {     
        this.format = format;     
    }     
    
    /**
     * 
     * @param value
     * @param JsonConfig
     */
    public Object processArrayValue(Object value, JsonConfig jsonConfig) {     
        String[] obj = {};     
        if (value instanceof Date[]) {     
            SimpleDateFormat sf = new SimpleDateFormat(format);     
            Date[] dates = (Date[]) value;     
            obj = new String[dates.length];     
            for (int i = 0; i < dates.length; i++) {     
                obj[i] = sf.format(dates[i]);     
            }     
        }     
        return obj;     
    }     
    
    /**
     * 
     * @param key
     * @param value
     * @param JsonConfig
     */
    public Object processObjectValue(String key, Object value, JsonConfig jsonConfig) {     
        if (value instanceof Date) {     
            return new SimpleDateFormat(format).format((Date) value);     
        }     
        return value == null ? null : value.toString();     
    }     
    
    public String getFormat() {     
        return format;     
    }     
    
    public void setFormat(String format) {     
        this.format = format;     
    }     
    
}